package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.enums.BWeightEnum;
import cn.huiyunche.base.service.interfaces.BAreaService;
import cn.huiyunche.base.service.interfaces.BPositionSubsidyService;
import cn.huiyunche.base.service.mappers.BPositionSubsidyMapper;
import cn.huiyunche.base.service.mappers.BWeightMapper;
import cn.huiyunche.base.service.model.BPositionSubsidy;
import cn.huiyunche.base.service.model.BPositionSubsidyExample;
import cn.huiyunche.base.service.model.BWeight;
import cn.huiyunche.base.service.model.BWeightExample;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class BPositionSubsidyServiceImpl implements BPositionSubsidyService {

    private static final Logger LOGGER = LoggerFactory.getLogger(BPositionSubsidyServiceImpl.class);

    @Autowired
    private BPositionSubsidyMapper bPositionSubsidyMapper;

    @Autowired
    private BWeightMapper bWeightMapper;

    @Autowired
    private BAreaService bAreaService;

    @Override
    public Map<Integer, BigDecimal> getList(List<Integer> codes) {
        LOGGER.info("getList params : {}", codes);
        if (CollectionUtils.isEmpty(codes)) {
            LOGGER.error("getList codes is empty");
            throw new IllegalArgumentException("权重编码不能为空");
        }

        BWeightExample example = new BWeightExample();
        example.createCriteria().andCodeIn(codes);
        List<BWeight> list = bWeightMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.stream().collect(Collectors.toMap(BWeight::getCode, BWeight::getWeight));
        }
        return null;
    }

    @Override
    public BigDecimal getSubsidy(BigDecimal result) {
        LOGGER.info("getSubsidy param : {}", result);
        if (null == result) {
            LOGGER.error("getSubsidy result is null");
            throw new IllegalArgumentException("位置级别综合系数不能为空");
        }

        BPositionSubsidyExample example = new BPositionSubsidyExample();
        example.createCriteria().andCoefficientBeginLessThanOrEqualTo(result).andCoefficientEndGreaterThanOrEqualTo(result);
        List<BPositionSubsidy> list = bPositionSubsidyMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0).getSubsidy();
        }

        return null;
    }

    @Override
    public BigDecimal computeSubsidy(String orginCode, String destinationCode) {
        LOGGER.info("computeSubsidy params : {}, {}", orginCode, destinationCode);
        if (StringUtils.isBlank(orginCode)) {
            LOGGER.error("computeSubsidy originCode is blank");
            throw new IllegalArgumentException("始发地编码不能为空");
        }
        if (StringUtils.isBlank(destinationCode)) {
            LOGGER.error("computeSubsidy destinationCode is blank");
            throw new IllegalArgumentException("目的地编码不能为空");
        }

        BigDecimal orginLevel = bAreaService.getCityCoefficientByCode(orginCode);
        BigDecimal destinationLevel = bAreaService.getCityCoefficientByCode(destinationCode);

        List<Integer> codes = Arrays.asList(BWeightEnum.ORIGIN.getValue(), BWeightEnum.DESTINATION.getValue());
        Map<Integer, BigDecimal> map = this.getList(codes);

        BigDecimal result = orginLevel.multiply(map.get(BWeightEnum.ORIGIN.getValue())).add(destinationLevel.multiply(map.get(BWeightEnum.DESTINATION.getValue())));

        return this.getSubsidy(result);
    }
}
